/*
 * Author: 李波(Edwin Lee)
 * Date: 2024-06-21 23:34:24
 * FilePath: /chat/src/server/model/groupmodel.cc
 * LastEditTime: 2024-07-11 00:14:13
 * version: 1.0
 * Description: 
 */
#include "group.hpp"
#include "groupmodel.h"
#include "connection.h"
#include "commonConnectionPool.h"
bool GroupModel::createGroup(Group& group)
{
    char sql[1024] = {0};
    sprintf(sql,"insert into allgroup(groupname,groupdesc) values('%s','%s')",group.getName().c_str(),group.getDesc().c_str());
    ConnectionPool& cnp = ConnectionPool::getInstance();
    shared_ptr<Connection> conn = cnp.getConnection();
    if(conn != nullptr)
    {
        if(conn->update(sql))
        {
            group.setId(conn->getInsertId());
            return true;
        }
    }
    return false;
}

void GroupModel::addGroup(int userid, int groupid, string role)
{
    char sql[1024] = {0};
    sprintf(sql,"insert into groupuser values(%d, %d, '%s')",groupid, userid, role.c_str());
    ConnectionPool& cnp = ConnectionPool::getInstance();
    shared_ptr<Connection> conn = cnp.getConnection();
    if(conn != nullptr)
    {
        conn->update(sql);
    }
}

vector<Group> GroupModel::queryGroups(int userid)
{
    char sql[1024] = {0};
    sprintf(sql,"select a.id,a.groupname,a.groupdesc from algroup a inner join (select groupid,userid \
        from groupuser where userid=%d) b on a.id=b.groupid",userid);
    vector<Group> vgroup;
    ConnectionPool& cnp = ConnectionPool::getInstance();
    shared_ptr<Connection> conn = cnp.getConnection();
    if(conn != nullptr)
    {
        MYSQL_RES* res = conn->query(sql);
        if(res != nullptr)
        {
            MYSQL_ROW row;
            while((row = mysql_fetch_row(res)) != nullptr)
            {
                Group group;
                group.setId(atoi(row[0]));
                group.setName(row[1]);
                group.setDesc(row[2]);
                vgroup.push_back(group);
            }
            mysql_free_result(res);
        }
    } 

    //查询群组的用户信息
    for(Group& group:vgroup)
    {
        sprintf(sql, "select a.id,a.name,a.state,b.grouprole from user a inner join \
            groupuser b on b.userid = a.id where b.groupid=%d",group.getId());

        MYSQL_RES* res = conn->query(sql);
        if(res != nullptr)
        {
            MYSQL_ROW row;
            while((row = mysql_fetch_row(res)) != nullptr)
            {
                GroupUser user;
                user.setId(atoi(row[0]));
                user.setName(row[1]);
                user.setState(row[2]);
                user.setRole(row[3]);
                group.getUsers().push_back(user);
            }
            mysql_free_result(res);
        }
    }
    return vgroup;
}

vector<int> GroupModel::queryGroupUsers(int userid, int groupid)
{
    char sql[1024] ={0};
    sprintf(sql, "select userid from groupuser where groupid=%d and userid != %d",groupid, userid);
    ConnectionPool& cnp = ConnectionPool::getInstance();
    shared_ptr<Connection> conn = cnp.getConnection();
    vector<int> vid;
    if(conn != nullptr)
    {
        MYSQL_RES* res = conn->query(sql);
        if(res != nullptr)
        {
            MYSQL_ROW row;
            while((row = mysql_fetch_row(res)) != nullptr)
            {
                vid.push_back(atoi(row[0]));
            }
            mysql_free_result(res);
        }
    }
    return vid;
}